#include "widget.h"
#include "ui_widget.h"
#include <QtWidgets/QMessageBox>
#include <QFileDialog>
#include <QDebug>
#include "fft.h"
QString import_path;
QString output_path;

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowTitle("FFT计算器");
    ui->textBrowser->append("Setup Success");
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_about_clicked()
{
    QMessageBox about;
    about.setWindowTitle("关于");
    about.setText("关于软件:\r\nBy:高宇航\r\nEmail:seeyounextsun@outlook.com\r\nClass:自动化211\r\nFor:信号分析与处理课设\r\nQT_Version:5.14.2\r\n\r\n软件使用:\r\n选择好分析数据的txt文件和FFT处理后生成数据的生成文件夹填写采样点(采样点数必须是2的指数)然后点击生成，等待日志框输出成功信息即可，输出文件名为FFTOutput.txt");
    about.setIconPixmap(QPixmap(":/image/logo.png"));
    about.addButton("我知道了", QMessageBox::AcceptRole);
    about.exec();
}

void Widget::on_pushButton_brower1_clicked()
{
    QString file_name = QFileDialog::getOpenFileName(this,"选择导入文件",".","*.txt");
    ui->lineEdit->setText(file_name);
    ui->textBrowser->append("import dir:\r\n"+file_name);
    import_path = file_name;
}

void Widget::on_pushButton_brower2_clicked()
{
    QString file_name2 = QFileDialog::getExistingDirectory(NULL,"选择输出文件夹",".");
    ui->lineEdit_2->setText(file_name2);
    ui->textBrowser->append("output dir:\r\n"+file_name2);
    output_path = file_name2;
}

void Widget::on_lineEdit_3_editingFinished()
{
    QString samp_point = ui->lineEdit_3->text();
    ui->textBrowser->append("samp point:\r\n"+samp_point);
    data_size = samp_point.toInt();
    //qDebug() << data_size;
}

void Widget::on_pushButton_calc_clicked()
{

    if(import_path == "\0")
    {
        ui->textBrowser->append("请选择导入文件");
    }
    else if(output_path == "\0")
    {
        ui->textBrowser->append("请选择输出路径");
    }
    else if(data_size<=0)
    {
        ui->textBrowser->append("请输入大于0的数");
    }
    else if(judge_2() == -1)
    {
        ui->textBrowser->append("请输入2的幂次的数");
    }
    else
    {
        TXT_Read();
        ui->textBrowser->append("正在读取导入文件...");

        transform();//变换序列顺序
        ui->textBrowser->append("正在变换序列顺序...");
        fft();//蝶形运算
        ui->textBrowser->append("正在进行蝶形计算...");
        output();//输出结果
        TXT_Write();
        ui->textBrowser->append("计算成功！数据保存在FFTOutput.txt文件");
    }
}

// 判断一个数是否为2的幂次
int judge_2(void)
{
    if ((data_size&(data_size - 1)))   //去掉一个1，判断是否为0
        return -1;
    return 1;
}
